package com.manager.assignment.zq.controller;




import com.manager.assignment.zq.service.GetSeller;
import com.manager.assignment.zq.service.SetSellerService;
import com.manager.assignment.zq.service.GetCusService;
import com.manager.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("/distribute")
@Api(tags= "分配管理")
public class CustomerAssignmentController {

    @Autowired
    private SetSellerService setSellerService;
    @Autowired
    private GetSeller getSeller;
    @Autowired
    private GetCusService getCusService;
    /**
     * 批量分配客户
     * @param map
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("批量分配客户")
    public Result assignCustomer(@RequestBody HashMap<String,Object> map){
//        System.out.println(map);
//        System.out.println(map.get("sellerId"));
        List<Integer> customerId = (List<Integer>)map.get("newCustomerIds");
        Integer sellerId = Integer.parseInt(map.get("sellerId").toString());
        for(int i = 0;i<customerId.size();i++)
        {
            setSellerService.SetSeller(customerId.get(i),sellerId);
        }
        return Result.ok();
    }

    /**
     * 批量移除分配的客户
     * @param map
     * @return
     */
    @ApiOperation("批量移除分配的客户")
    @PostMapping("/reset")
    public Result resetCustomer(@RequestBody HashMap<String,Object> map){
        List<Integer> customerId = (List<Integer>)map.get("customerIds");
        for(int i = 0;i<customerId.size();i++)
        {
            setSellerService.RemoveCustomer(customerId.get(i));
        }
        return Result.ok();
    }

    /**
     * 获取所有销售的id与name
     * @return
     */
    @ApiOperation("获取所有销售的id与name")
    @GetMapping("/get/seller")
    public Result getAllSellers(){
        return Result.ok(getSeller.getAllSellers());
    }

    /**
     * 获取销售名下的客户id
     * @param sellerId
     * @return
     */
    @ApiOperation("获取销售名下的客户id")
    @GetMapping("/get/cus")
    public Result getCusBySellerId(@RequestParam Integer sellerId){
        return Result.ok(getCusService.getCusBySellerId(sellerId));
    }
    @ApiOperation("获取新客户")
    @GetMapping("/get/newcus")
    public Result getNewCus(){
        return Result.ok(getCusService.getNewCus());
    }
}
